//主界面框架
Ext.ns('Logisticext.frame');
Logisticext.frame.doLayout = function() {
    F = Logisticext.frame = Ext.create('Ext.Viewport', {
        id: 'viewport',
        layout: 'border',
        items: [{
                xtype: 'toolbar',
                region: 'north',
                defaultType: 'button',
                height: 46,
                border: false,
                style: 'background:transparent',
                items: ['Third-party LOGISTICS  System', '->', {
                        text: '消息',
                        xtype: 'splitbutton',
                        menu: {
                            items: [
                                // these will render as dropdown menu items when the arrow is clicked:
                                {
                                    text: '小红修改了角色',
                                    handler: function() {
                                        alert("TODO：回复小红的消息");
                                    }
                                },
                                {
                                    text: 'BOSS：小强，做的怎么样了？',
                                    handler: function() {
                                        alert("TODO：回复BOSS的消息");
                                    }
                                }, '-', {
                                    text: '全部标记为已读',
                                    handler: function() {
                                        alert("TODO：标记为已读，并清空列表");
                                    }
                                }, {
                                    text: '查看历史记录',
                                    handler: function() {
                                        alert("TODO：弹出历史记录窗口");
                                    }
                                }
                            ]
                        },
                        handler: function() {
                            Ext.create('Logisticext.ui.ChatWindow', {
                            }).show();
                        }
                    }, {
                        text: '修改密码',
                        handler: function() {

                        }
                    }, ' ', {
                        text: '安全退出',
                        handler: function() {
                            top.window.location = BASE_URL + '/identity/quit';
                        }
                    }]
            }, Ext.create('Logisticext.ui.ChatWindow', {
                // lazily created panel (xtype:'panel' is default)
                region: 'east',
                //collapsed: true,
                //split: true,
                collapsible: true,
               // animCollapse: true,
                width: 180,
                margins: '0 0 0 0'
            }), {
                region: 'west',
                stateId: 'navigation-panel',
                itemId: 'menu',
                title: '功能面板',
                split: true,
                width: 200,
                minWidth: 175,
                maxWidth: 400,
                collapsible: true,
                animCollapse: true,
                margins: '0 0 0 5',
                layout: 'accordion',
                items: [],
                listeners: {
                    render: function() {
                        var _this = this;
                        Ext.Ajax.request({
                            url: BASE_URL + '/identity/menu',
                            success: function(response) {
                                var text = response.responseText;
                                var result = Ext.JSON.decode(text);

                                for (var i = 0; i < result.planforms.length; i++) {
                                    var menu = [];
                                    var p = result.planforms[i];
                                    var pcfg = {
                                        itemId: p.id,
                                        title: p.name,
                                        iconCls: p.iconCls,
                                        xtype: 'treepanel',
                                        rootVisible: false,
                                        listeners: {
                                            itemclick: function(view, re) {
                                                F.switchModule(re.raw);
                                            }
                                        },
                                        root: {
                                            expanded: true,
                                            children: []
                                        }
                                    };
                                    for (var j = 0; j < p.modules.length; j++) {
                                        var m = p.modules[j];
                                        var mcfg = {
                                            text: m.name,
                                            leaf: true,
                                            itemId: m.code,
                                            perms: {}
                                        };
                                        for (var k = 0; k < m.permissionList.length; k++) {
                                            var pm = m.permissionList[k];
                                            mcfg.perms[pm.key] = pm.name;
                                        }
                                        pcfg.root.children.push(mcfg);
                                    }
                                    menu.push(pcfg);
                                    _this.add(menu);
                                }
                                _this.doLayout();
                            }
                        });
                    }
                }
            },
            // in this instance the TabPanel is not wrapped by another panel
            // since no title is needed, this Panel is added directly
            // as a Container
            Ext.create('Ext.tab.Panel', {
                region: 'center', // a center region is ALWAYS required for border
                itemId: 'mainTab',
                // layout
                deferredRender: false,
                activeTab: 0, // first tab initially active
                defaults: {
                },
                items: [{
                        contentEl:'abc'
                }]
            })]
    });

    // aliases
    F.mainTab = F.getComponent('mainTab');
    F.menu = F.getComponent('menu');

    // 方法:切换模块
    F.switchModule = function(module) {
        var exists = F.mainTab.getComponent(module.itemId);
        if (!exists) {
            exists = Ext.create('Logisticext.module.' + module.itemId, {
                itemId: module.itemId,
                title: module.text,
                perms: module.perms,
                closable: true
            });
            F.mainTab.add(exists);
        }
        F.mainTab.setActiveTab(exists);
    };
    F.url = function(url) {
        return BASE_URL + url;
    };
};
